<template>
	<view>
		音量检测示例
	</view>
	<view :style="{
		width:audioLevel + 'px',
		height:'10px',
		backgroundColor:'#8dc63f',
		marginTop:'20px',
	}"> 
	</view>

	
	
</template>

<script>
	import sm from '@/thirdjs/soundmeter.js'
	export default {
		data() {
			return {
				audioLevel:0,
			}
		},
		onLoad() {
			console.log('11111',sm);
			try{
				window.AudioContext = window.AudioContext || window.webkitAudioContext;
				window.audioContext = new AudioContext();
			}catch(e){
				console.log('网页音频API不支持.')
			}
			
			soundMeter = window.soundMeter = new sm.SoundMeter(window.audioContext);
			
			const constraints  = window.constraints = {
				audio:true,
				video:false,
			}
			navigator.mediaDevices.getUserMedia(constraints)
				.then(this.handleSuccess)
				.catch(this.handleError);
		},
		methods: {
			handleSuccess(stream){
			    window.stream = stream;
				soundMeter.connectToSource(stream);
				setTimeout(this.soundMeterProcess,100);
			},	
			handleError(error){
			    console.log(error);
			},
			soundMeterProcess(){
			        var val = (window.soundMeter.instant.toFixed(2) * 348) + 1;
			        console.log(val);
					this.audioLevel = val;
			        setTimeout(this.soundMeterProcess,100);
			}
		}
	}
</script>

<style>

</style>
